<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加告警设备' : '编辑告警设备'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称：" prop="companyId">
              <el-select v-model="ruleForm.companyId" placeholder="请选择公司名称" clearable @change="companyChange">
                <el-option v-for="item in companyArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="基地名称：" prop="baseId">
              <el-select v-model="ruleForm.baseId" placeholder="请选择基地名称" @change="baseIdChange" clearable>
                <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="告警模板：" prop="modelAlarmSettingsId">
              <el-select v-model="ruleForm.modelAlarmSettingsId" placeholder="请选择告警模板" clearable :disabled="seeType">
                <el-option v-for="item in modelAlarmSettingsArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="控制箱：" prop="boxId">
              <el-select v-model="ruleForm.boxId" placeholder="请选择控制箱" clearable :disabled="seeType" @change="monitorChange">
                <el-option v-for="item in monitorArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="开关设备：" prop="deviceId">
              <el-select v-model="ruleForm.deviceId" placeholder="请选择开关设备" clearable :disabled="seeType">
                <el-option v-for="item in openArray" :key="item.id" :label="item.deviceName"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="数值设备：" prop="deviceId">
              <el-select v-model="ruleForm.deviceId" placeholder="请选择数值设备" clearable :disabled="seeType">
                <el-option v-for="item in deviceArray" :key="item.id" :label="item.deviceName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getModelAlarmDevice, saveModelAlarmDevice, getModelAlarmSettingss } from '@/api/alarm'
// import { getCompanyInfos } from "@/api/company";
import { getCompanyInfoTree } from '@/api/company'

// import { getMonitorDevices } from "@/api/monitor";
// import { getMonitorInfos } from "@/api/monitor";
import { getObjBoxs, getSysCompanyBases, getConfigFishpondDeviceNumericalRelations, getConfigFishpondDeviceSwitchRelations } from '@/api/system'

export default {
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        companyId: '',
        modelAlarmSettingsId: '',
        monitorId: '',
        deviceId: '',
        boxId: ''
      },
      rules: {
        // 表单规则
        companyId: [{ required: true, message: '请选择公司名称', trigger: 'change' }],
        baseId: [{ required: true, message: '请选择基地名称', trigger: 'change' }],
        modelAlarmSettingsId: [{ required: true, message: '请选择告警模板', trigger: 'change' }],
        boxId: [{ required: true, message: '请选择控制箱', trigger: 'change' }],
        deviceId: [{ required: true, message: '请选择设备', trigger: 'change' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      companyArray: [], // 机构数组
      modelAlarmSettingsArray: [], // 告警模板数组
      deviceArray: [], // 监测设备数组
      openArray: [],
      monitorArray: [], // 监测点数组
      userType: '', // 账号类型（0超级管理员，2组织管理员，3一般管理员）
      companyId: '', // 机构id
      baseArray: []
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取机构列表
        this.getCompanyInfoTree()

        // 获取公司列表
        //  this.getCompanyInfoTree();
      }
      // // 添加
      // if (this.addEditStatus == "add") {
      //   this.ruleForm.id = "";
      //     this.seeType = false;
      // }

      // // 编辑
      // if (this.addEditStatus == "edit") {
      //   this.ruleForm.id = this.addEditId;
      //   this.seeType = false;
      //   this.getDetail();
      //   console.log(1);
      // }

      // // 查看
      // if (this.addEditStatus == "see") {
      //   this.ruleForm.id = this.addEditId;
      //   this.seeType = true;
      //   // this.getDetail();
      // }
      // this.userType = Number(window.localStorage.getItem("userType"));

      // // // 获取公司列表
      // this.getCompanyInfoTree();
    }
  },
  methods: {
    // 详情
    getDetail() {
      console.log(1)
      getModelAlarmDevice({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data

          this.getSysCompanyBases()

          // 获取告警模板列表

          this.getModelAlarmSettingsList()
          // 获取监测点列表
          this.getMonitorInfoList()
          // 获取监测设备列表
          this.getMonitorDeviceList()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveModelAlarmDevice(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 取消
              this.resetForm(formName)
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      console.log(this.ruleForm)
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // // 获取机构列表
    // getCompanyList() {
    //   getCompanyInfos().then((res) => {
    //     if (res.code == 200) {
    //       this.companyArray = res.data;
    //     }
    //   });
    // },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === '') {
        return
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: '总公司',
            parentId: 0
          }
        ]
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data
          }
        })
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1]
      } else {
        this.ruleForm.companyId = ''
      }

      this.ruleForm.baseId = ''
      this.baseArray = []
      console.log(val)
      // this.getObjFishTypes(val)
      // 获取基地列表
      this.getSysCompanyBases()
    },
    // 获取基地列表
    getSysCompanyBases() {
      console.log(1)

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data
        }
      })
    },
    // 机构下拉切换
    companyChange() {
      // 清空
      this.ruleForm.modelAlarmSettingsId = ''
      this.modelAlarmSettingsArray = []

      this.ruleForm.monitorId = ''
      this.monitorArray = []

      this.ruleForm.deviceId = ''
      this.deviceArray = []
      this.openArray = []
      // 获取基地列表
      this.getSysCompanyBases()
      // 获取告警模板列表
      this.getModelAlarmSettingsList()
    },
    baseIdChange() {
      // 获取监测点列表
      this.getMonitorInfoList()
    },
    // 获取告警模板列表
    getModelAlarmSettingsList() {
      if (!this.ruleForm.companyId || this.ruleForm.companyId == '') {
        return
      }
      getModelAlarmSettingss({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.modelAlarmSettingsArray = res.data
        }
      })
    },
    // 获取监测点列表
    getMonitorInfoList() {
      if (!this.ruleForm.baseId || this.ruleForm.baseId == '') {
        return
      }
      getObjBoxs({ id: this.ruleForm.baseId }).then((res) => {
        if (res.code == 200) {
          this.monitorArray = res.data
        }
      })
    },
    // 监测点下拉切换
    monitorChange() {
      this.ruleForm.deviceId = ''
      this.deviceArray = []
      this.openArray = []

      // 获取监测设备列表
      this.getMonitorDeviceList()
    },
    // 获取监测设备列表
    getMonitorDeviceList() {
      if (!this.ruleForm.boxId || this.ruleForm.boxId == '') {
        return
      }
      getConfigFishpondDeviceNumericalRelations({ id: this.ruleForm.boxId }).then((res) => {
        if (res.code == 200) {
          this.deviceArray = res.data
          console.log(res.data)
        }
      })

      getConfigFishpondDeviceSwitchRelations({ id: this.ruleForm.boxId }).then((res) => {
        if (res.code == 200) {
          this.openArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}
</style>
